<template>
  <ContentWrap>
    <!-- 子表模式下不显示搜索栏 -->
    <el-button type="primary" @click="openForm('create')" style="margin-bottom: 10px;">新增工资标准</el-button>
    <el-button
      v-if="props.userId"
      type="success"
      @click="handleBatchGenerate"
      style="margin-left: 10px; margin-bottom: 10px;"
    >批量生成工资标准</el-button>
    <br/>
    <el-text type="danger">备注：批量生成是根据员工信息里填入的入职日期和结束日期，以及基本工资、岗位工资、试用期工资来计算的</el-text>
  </ContentWrap>
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="员工名称" align="center" prop="userName" />
      <el-table-column label="年份" align="center" prop="years" />
      <el-table-column label="基本工资" align="center" prop="baseSalary" />
      <el-table-column label="岗位工资" align="center" prop="positionSalary" />
      <el-table-column label="试用期工资" align="center" prop="probationSalary" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)">编辑</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
  <SalaryStandardForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter,formatDate2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { SalaryStandardApi, SalaryStandardVO } from '@/api/oa/salarystandard'
import SalaryStandardForm from './SalaryStandardForm.vue'
import { defineProps, watch } from 'vue'

const props = defineProps<{ userId?: number, userName?: string, years?: number,hireDate?: Date,endDate?: Date,baseSalary?: number,positionSalary?: number,probationSalary?: number }>()

/** 工资标准管理 列表 */
defineOptions({ name: 'SalaryStandard' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SalaryStandardVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: props.userId,
  userName: props.userName,
  years: props.years,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 子表模式下自动带入userId、userName、years
    if (props.userId) queryParams.userId = props.userId
    if (props.userName) queryParams.userName = props.userName
    if (props.years) queryParams.years = props.years
    const data = await SalaryStandardApi.getSalaryStandardPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

watch([() => props.userId, () => props.userName, () => props.years], ([userId, userName, years]) => {
  if (userId) queryParams.userId = userId
  if (userName) queryParams.userName = userName
  if (years) queryParams.years = years
  getList()
}, { immediate: true })

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  // 子表模式下新增时传递userId、userName、years
  if (type === 'create' && props.userId) {
    formRef.value.open(type, undefined, props.userId, props.userName, props.years)
  } else {
    formRef.value.open(type, id)
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SalaryStandardApi.deleteSalaryStandard(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SalaryStandardApi.exportSalaryStandard(queryParams)
    download.excel(data, '工资标准管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleBatchGenerate = async () => {
  // 必填校验
  if (!props.hireDate) {
    message.error('入职日期不能为空')
    return
  }
  if (!props.endDate) {
    message.error('结束日期不能为空')
    return
  }
  if (props.baseSalary == null || props.baseSalary === undefined) {
    message.error('基本工资不能为空')
    return
  }
  try {
    await message.confirm('确定要为该员工批量生成工资标准吗？')
    await SalaryStandardApi.generateSalaryStandard({
      userId: props.userId!,
      hireDate: formatDate2(props.hireDate),
      endDate: formatDate2(props.endDate),
      baseSalary: props.baseSalary,
      positionSalary: props.positionSalary,
      probationSalary: props.probationSalary,
    })
    message.success('批量生成成功')
    getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
